<?php
namespace MyApp\Application\Controller;


use MyApp\Application\Model\HoaDonModel;
use DinhThi\MVC\Request;

class HoaDonController extends MyAppAbstractController{
    public function __construct(){
        parent::__construct();
        parent::mustBeLogin();
    }

    public function indexAction(){

        $this->view->setTitle('Quản lý bán hàng');
        $this->view->render('HoaDon/index');
    }

    public function inAction(){

        $this->view->setTitle('Quản lý bán hàng');
        $this->view->render('HoaDon/in', true);
    }

    public function DanhSachAction(){

        $this->view->addHeaderScript('Public/js/HoaDon/DanhSachHoaDon.js');

        $this->view->setTitle('Danh sách hóa đơn');
        $this->view->render('HoaDon/danh-sach');
    }

    public function ChonMuaAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');
        $this->view->addHeaderScript('Public/js/HoaDon/ChonMua.js');


        $this->view->addHeaderScript('Public/plugin/JsTree/dist/jstree.min.js');
        $this->view->addHeaderStyle('Public/plugin/JsTree/dist/themes/default/style.min.css');

        $this->view->setTitle('Chọn hàng để bán');
        $this->view->render('HoaDon/chon-mua');
    }

    public function ThanhToanAction(){

        $request = Request::getInstance();
        if (isset($request->getParams()[2])){
            $id = $request->getParams()[2];
            $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');
            $this->view->addHeaderScript('Public/js/HoaDon/ThanhToanHoaDon.js');

            $model = new HoaDonModel();
            $data = $model->ThongTinHoaDon($id);

            $this->view->TongTien = $data['trigia'];
            $this->view->MaHoaDon = $data['id'];

            $this->view->setTitle('Thanh toán hóa đơn');
            $this->view->render('HoaDon/thanh-toan');
        }
    }


    /*==========Ajax================*/
    public function ChiTietHoaDonAction(){
        $request = Request::getInstance();
        if (isset ($request->getParams()[2])){
            $model = new HoaDonModel();
            echo json_encode($model->ChiTietHoaDon($request->getParams()[2]));
        }
    }

    public function TaoHoaDonAction(){
        if (isset ($_POST['trigia'])){
            $model = new HoaDonModel();

            $trigia = $_POST['trigia'];
            $giagoc = $_POST['giagoc'];

            $listMatHang = $_POST['listMatHang'];
            $listGiaGoc = $_POST['listGiaGoc'];
            $listGiaBan = $_POST['listGiaBan'];
            $listSoLuong = $_POST['listSoLuong'];
            $listThanhTien = $_POST['listThanhTien'];

            $id = $model->TaoHoaDon($trigia, $giagoc, $listMatHang, $listGiaGoc, $listGiaBan, $listSoLuong, $listThanhTien);
            echo $id;
        }
    }

    public function TaoThanhToanAction(){
        if (isset ($_POST['id'])){
            //echo json_encode($_POST);
            $model = new HoaDonModel();

            $id = $_POST['id'];
            $KhachHang = $_POST['KhachHang'];
            $ConLai = $_POST['TienConLai'];
            $GhiChu = $_POST['GhiChu'];

            $model->TaoThanhToan($id, $KhachHang, $ConLai, $GhiChu);
        }
    }

    public function HuyHoaDonAction(){
        $request = Request::getInstance();
        if (isset ($request->getParams()[2])){
            $model = new HoaDonModel();
            $model->HuyHoaDon($request->getParams()[2]);
        }
    }

    public function DanhSachHoaDonAction(){
        if (isset ($_POST['begin'])){
            $model = new HoaDonModel();

            $begin = $_POST['begin'];
            $end = $_POST['end'];

            $begin = str_replace('/', '-', $begin);
            $begin = date('Y-m-d', strtotime($begin)) . ' 00:00:00';

            $end = str_replace('/', '-', $end);
            $end = date('Y-m-d', strtotime($end)) . ' 23:59:59';

            $data = array(
                'ChuaThanhToan' => $model->DSHoaDonChuaThanhToan($begin, $end),
                'DaThanhToan'   => $model->DanhSachHoaDon($begin, $end)
            );

            echo json_encode($data);
        }
    }
    public function TatCaHoaDonAction(){
        $model = new HoaDonModel();

        $data = array(
            'DaThanhToan' => $model->TatCaHoaDon(),
            'ChuaThanhToan'   => $model->TatCaHoaDonChuaTT()
        );

        echo json_encode($data);
    }
} 